(function (p) { /* Sai.Calendar.Agenda */

    var Agenda = function ( container ) {

        this.container  = container;
        this.menu       = new Sai.Calendar.Agenda.Menu(container.find("div.menu"));
        this.list       = new Sai.Calendar.Agenda.List(container.find("ul.days"));
        this.filtering  = false;

        this.menu.addEventListener(Sai.Calendar.Agenda.Menu.ON_SHOW_ALL, $.proxy(this.onMenuShowAll, this));
        this.menu.addEventListener(Sai.Calendar.Agenda.Menu.ON_HIDE_ALL, $.proxy(this.onMenuHideAll, this));
        this.menu.addEventListener(Sai.Calendar.Agenda.Menu.ON_APPLY_FILTER, $.proxy(this.onMenuApplyFilter, this));
        this.menu.addEventListener(Sai.Calendar.Agenda.Menu.ON_CLEAR_FILTER, $.proxy(this.onMenuClearFilter, this));

        /*var date = new Date(2014, 03, 0)
            date.getDate()
        console.log(date.getDate())*/
        //this.menu.filter("22/03/2009", "24/03/2009");
        
    }
    Agenda.fn = Agenda.prototype;


    EventDispatcher.prototype.apply(Agenda.prototype);


    Agenda.fn.setData = function ( data ) {
        
        this.data           = data;
        this.currentData    = data;
        
        //this.list.load(data);

    }


    Agenda.fn.setCurrentDate = function ( year, month ) {
        
        //console.log("Agenda.fn.setCurrentDate - year:"+year+" - month:"+month);
        var from        = new Date(year, month);
        var monthDays   = (new Date(year, month+1, 0)).getDate();
        //console.log(monthDays);
        var to = new Date(from.getTime());
            to.setDate(monthDays);
            //to.setMonth
        //console.log("Agenda.fn.setCurrentDate: "+from+" "+to);
        this.currentYear        = year;
        this.currentMonth       = month;
        this.currentMonthData   = this.getDataSelection(from.getTime(), to.getTime());
        //console.log(this.currentMonthData);
        if( !this.menu.filtering ){
            this.list.load(this.currentMonthData);
        }

    }


    Agenda.fn.getDataSelection = function ( from, to ) {
        
        //console.log("Agenda.fn.getDataSelection - from:"+from+" - to:"+to);
        var selection   = [];
        var from        = new Date(from);
        var to          = new Date(to);
        
        for(var i=0; i < this.data.length; i++){
            //console.log(Number(this.data[i].date) + " " + from.getTime());
            if(Number(this.data[i].date) > from.getTime() &&
               Number(this.data[i].date) < to.getTime()) {
                
                selection.push(this.data[i]);
                
            }               
            
        }
        
        return selection;
        
    }


    Agenda.fn.filter = function ( from, to ) {
        
        var selection = this.getDataSelection(from, to);
        
        this.list.load(selection);
        
        this.menu.filter(from, to);
        
    }


    Agenda.fn.onMenuShowAll = function () {

        this.list.showAll();

    }


    Agenda.fn.onMenuHideAll = function () {

        this.list.hideAll();

    }


    Agenda.fn.onMenuApplyFilter = function ( event ) {
        
        //console.log(event);
        this.filter(event.from, event.to);
        
    }
    

    Agenda.fn.onMenuClearFilter = function () {

        this.list.load(this.currentMonthData);
        
    }

    Agenda.MONTH_SELECT = "onMonthSelect";
    Agenda.AGENDA_SELECT = "onMonthSelect";

    p.Agenda = Agenda;

})(Sai.Calendar);